// 6_9.c Coin Toss Simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int flip(int);              /*prototype for flip function*/
void percentages(int, int); /*prototype for percentage function*/
int main()
{
    int numTosses = 1000;
    int heads;

    heads = flip(numTosses);
    percentages(numTosses, heads);

    return 0;
}

//this method tossed the coin numTimes
//and returns the number of heads
int flip(int numTimes)
{
    int randValue;
    int heads = 0;
    int i;

    srand(time(NULL));

    for (i = 1; i <= numTimes; i++)
    {
        randValue = 1 + (int)rand() % 100;
        if (randValue > 50)
            heads++;
    }

    return heads;
}

//this method calculates and displays
//the percentages of heads and tails
void percentages(int numTossess, int heads)
{
    int tails;
    float perheads, pertails;

    if(numTossess==0)
    printf("There were no tossess, so no percentages can be calculated.\n");
    else
    {
        tails=numTossess-heads;
        printf("Number of coin tosses: %d\n",numTossess);
        printf("    Heads:%d   Tails: %d\n",heads,tails);
        perheads=(float)heads/numTossess *100.0;
        pertails=(float)(numTossess-heads)/numTossess*100.0;
        printf("Heads came up %6.2f percent of the time.\n",perheads);
        printf("Tails came up %6.2f percent of the time.\n",pertails);
    }
}